<template>
  <el-form
      :model="caipin"
      label-position="right"
      label-width="100px"
      style="padding: 20px;"
  >
    <h1 style="color: #f58147;font-size: 20px">修改菜品信息</h1>
    <el-card style="box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05); border-radius: 8px;">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="菜品名称">
            <el-input v-model="caipin.cpmc" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜品价格">
            <el-input v-model="caipin.cpjg" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="菜品状态">
            <el-select v-model="caipin.status" placeholder="请选择状态" style="width: 100%;">
              <el-option label="上架" value="0"></el-option>
              <el-option label="已下架" value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜品描述">
            <el-input v-model="caipin.cpms" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="月销售量">
            <el-input v-model="caipin.count" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="菜品图片">
            <el-upload
                class="upload-demo"
                action="http://localhost:8080/upload"
                :file-list="fileList"
                list-type="picture"
                :on-success="handleSuccess"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                :limit="1"
                :auto-upload="false"
                style="width: 100%;"
            >
              <el-button size="small" type="primary" style="background-color: #f58147; border-color: #f58147;">选取文件</el-button>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-card>

    <el-row style="margin-top: 20px; display: flex;justify-content: center;">
      <el-button type="primary" @click="doSave" style="background-color: #f58147; border-color: #f58147;">保存</el-button>
      <el-button @click="doCancel">取消</el-button>
    </el-row>
  </el-form>
</template>


<script setup lang="ts">
import {ref} from 'vue'
import {ElMessage} from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits(['flushPage', 'closeDialog']);

const caipin = ref({
  cpmc: '',
  cpjg: '',
  cptp: '',
  cpms: '',
  status: '',
  count: ''
})

//-------------------------回显菜品信息------------------------------
const handleUpdateData = (obj) => {
  caipin.value = obj;
}

// 暴露给父组件使用的方法
defineExpose({
  handleUpdateData,
})


//---------------------------执行保存操作----------------------------
async function doSave() {
  const {data} = await axios.put('/api/caiPin', caipin.value);
  if (data.code === 200) {
    emits("flushPage");				// 触发事件： 刷新页面
    emits("closeDialog");			// 触发事件：关闭 dialog
    ElMessage.success("操作成功！");
  } else {
    ElMessage.error("操作失败！");
  }
}

//---------------------------取消操作----------------------------
function doCancel(){
  emits("closeDialog");			// 触发事件：关闭 dialog
}

const fileList = ref([]);

const handleSuccess = (response, file) => {
  if (response.code === 200) {
    caipin.value.cptp = response.data; // 假设返回的图片路径在 response.data 中
    ElMessage.success("上传成功");
  } else {
    ElMessage.error("上传失败");
  }
};

const handlePreview = (file) => {
  console.log("预览图片:", file);
};

const handleRemove = (file, fileList) => {
  console.log("移除图片:", file, fileList);
};


</script>

<style scoped>
.el-card {
  margin-bottom: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.el-form-item__content {
  width: 100%;
}

.el-input__inner {
  border-radius: 6px;
  border: 1px solid #e4e7ed;
  padding: 10px;
  transition: all 0.3s ease;
}

.el-input__inner:focus {
  border-color: #f58147;
  box-shadow: 0 0 5px rgba(245, 129, 71, 0.3);
}

.el-select {
  width: 100%;
}

.el-button--primary {
  background-color: #f58147;
  border-color: #f58147;
  transition: all 0.3s ease;
}

.el-button--primary:hover {
  background-color: #e0743f;
  border-color: #e0743f;
}
</style>
